/*
 * Purpose: represents the proxy of the editor view - a JLabel
 * or the tabbedPane... depending on the mode
 *
 *  This is a mixin pattern of sorts :)
 *
 *  I probably could implement EditorPanel extending JComponent
 *  and then override JComponent methods based on the current state of the editorPanel... (label or tabs)
 */
package View;

import Model.Programmable;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

/**
 *
 * @author jase
 */
public class EditorPanel {

	private JLabel label;
	private JTabbedPane tabs;
	private boolean active;

	public EditorPanel() {
		active = false;
		label = new JLabel("no project open");
		label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 24));
		label.setHorizontalAlignment(JLabel.CENTER);
		tabs = new JTabbedPane();
	}

	public JComponent asComponent() {
		if (active) {
			return tabs;
		}
		return label;
	}

	public void setText(String value) {
		label.setText(value);
	}

	public void enableTabs(boolean value) {

		if (!value) {
			tabs.removeAll(); // if we are switch from tabs, remove all the tab elements
		}
		active = value;
	}

	public Programmable add(String name) {

		// assuming tab not already exists!
		SourceCodePane coder = new SourceCodePane();
		coder.setName(name);
		coder.activateBehaviour();
		tabs.addTab(name, coder);
		tabs.invalidate();
		return coder;
	}

	public Programmable add(String name, String text) {
		SourceCodePane coder = new SourceCodePane();
		coder.setName(name);
		coder.setText(text);
		coder.activateBehaviour();
		tabs.addTab(name, coder);
		tabs.invalidate();
		return coder;
	}

	public void removeAll() {
		System.out.println("remove all");
		for (Component comp : tabs.getComponents()) {
			SourceCodePane p = (SourceCodePane) comp;
			p.shutdown();
		}
	}

	public void remove(String name) {
		for (Component comp : tabs.getComponents()) {
			SourceCodePane p = (SourceCodePane) comp;
			if (name.equals(p.getName())) {
				tabs.remove(tabs.indexOfComponent(comp));
				p.shutdown();
				return;
			}
		}
	}
}
